home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / tutorials / geometer / gettoken.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  4.3 KB  |  213 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. #include <ctype.h>
  19. #include "parse.h"
  20. #include "string.h"
  21.  
  22. char texttoken[200];
  23. long textptr;
  24.  
  25. float    floattoken;
  26. long    longtoken;
  27. long    linecount = 1;
  28.  
  29. char *fakefile = 0;
  30. char *fakefileptr;
  31.  
  32. void parseerror(char *s)
  33. {
  34.     char ss[120];
  35.     sprintf(ss, "Line %d: %s\n", linecount, s);
  36.     displaymessage(ss);
  37. }
  38.  
  39. void parseerror(char *s, long line)
  40. {
  41.     char ss[120];
  42.     sprintf(ss, "Line %d: %s\n", line, s);
  43.     displaymessage(ss);
  44. }
  45.  
  46. void initfakefile(char *foo)
  47. {
  48.     fakefile = fakefileptr = foo;
  49. }
  50.  
  51. int tomsgetc(FILE *fp)
  52. {
  53.     if (fp)
  54.         return getc(fp);
  55.     if (*fakefileptr)
  56.         return (int)*fakefileptr++;
  57.     return EOF;
  58. }
  59.  
  60. void tomsungetc(int c, FILE *fp)
  61. {
  62.     if (fp)
  63.         ungetc(c, fp);
  64.     else
  65.         fakefileptr--;
  66. }
  67.  
  68. static void readidstuff(FILE *fp, char *s)
  69. {
  70.     char c;
  71.     while(1) {
  72.     c = tomsgetc(fp);
  73.     if (isalnum(c) || c == '_' || c == '.')
  74.         *s++ = c;
  75.     else {
  76.         *s = 0;
  77.         if (c != EOF) tomsungetc(c, fp);
  78.         return;
  79.     }
  80.     }
  81. }
  82.  
  83. static void readreservedword(FILE *fp, char c)
  84. {
  85.     texttoken[0] = c;
  86.     readidstuff(fp, &texttoken[1]);
  87. }
  88.  
  89. // The code that inserts a file simply sets the prefix to some
  90. // non-empty string for file insertion.  This avoids (somewhat)
  91. // the problem with identifier conflicts.  I decided a postfix
  92. // was better.
  93.  
  94. extern char *suffix;
  95.  
  96. static void readidentifier(FILE *fp, char c)
  97. {
  98.     texttoken[0] = c;
  99.     readidstuff(fp, &texttoken[1]);
  100.     if (suffix) {
  101.     strcat(texttoken, suffix);
  102.     }
  103. }
  104.  
  105. static tokentype readnumber(FILE *fp, char c)
  106. {
  107.     char numstr[200], *nptr;
  108.     long isint = 1;
  109.     nptr = &numstr[0];
  110.     do {
  111.     if (c == '.' || c == '-' || c == '+' || c == 'e' || c == 'E')
  112.         isint = 0;
  113.     *nptr++ = c;
  114.     c = tomsgetc(fp);
  115.     } while ( isdigit(c) || c == '.' || c == '-'
  116.                     || c == '+' || c == 'e' || c == 'E');
  117.     if (c != EOF) tomsungetc(c, fp);
  118.     *nptr = 0;
  119.     if (isint) {
  120.         sscanf(numstr, "%d", &longtoken);
  121.     return Integer;
  122.     } else {
  123.         sscanf(numstr, "%g", &floattoken);
  124.     return Float;
  125.     }
  126. }
  127.  
  128. tokentype handledot(FILE *fp)
  129. {
  130.     char c = tomsgetc(fp);
  131.     if (isdigit(c)) {
  132.     tomsungetc(c, fp);
  133.     return readnumber(fp, '.');
  134.     }
  135.     tomsungetc(c, fp);
  136.     readreservedword(fp, '.');
  137.     return Reserved;
  138. }
  139.  
  140. static long readString(FILE *fp)
  141. {
  142.     char *s = texttoken;
  143.     char c;
  144.     
  145.     while (1) {
  146.     c = tomsgetc(fp);
  147.     if (c == '"') { *s = 0; return 1; }
  148.     if (c == '\n' || c == EOF) {
  149.         parseerror("Error: newline or EOF in string.");
  150.         *texttoken = 0;
  151.         return 0;
  152.     }
  153.     *s++ = c;
  154.     }
  155. }
  156.  
  157. tokentype gettoken(FILE *fp)
  158. {
  159.     int c;
  160.  
  161.     // skip white space
  162.     while (1) {
  163.     c = tomsgetc(fp);
  164.     if (c == '\n') linecount++;
  165.     if (c == EOF) return Eof;
  166.     if (c == '/') {
  167.         if (tomsgetc(fp) != '/') {
  168.         parseerror("Bad comment");
  169.         return Error;
  170.         }
  171.         textptr = 0;
  172.         while (1) {
  173.         texttoken[textptr++] = c = tomsgetc(fp);
  174.         texttoken[textptr] = (char)0;
  175.         if (c == '\n') {linecount++; return Comment;}
  176.         if (c == EOF) return Comment;
  177.         }
  178.     }
  179.     if (isspace(c) == 0) break;
  180.     }
  181.     switch (c) {
  182.         case ',':
  183.         return Comma;
  184.     case ';':
  185.         return Semicolon;
  186.     case '=':
  187.         return Equal;
  188.     case '(':
  189.         return Open;
  190.     case ')':
  191.         return Close;
  192.     case '{':
  193.         return OpenCurly;
  194.     case '}':
  195.         return CloseCurly;
  196.     case '"':
  197.         if (readString(fp))
  198.             return Str;
  199.         return Error;
  200.     case '.':
  201.         return handledot(fp);
  202.     default:
  203.         if (isdigit(c) || c == '-')
  204.             return(readnumber(fp, c));
  205.             if (isalpha(c)) {
  206.             readidentifier(fp, c);
  207.             return Identifier;
  208.         }
  209.         parseerror("Garbage character");
  210.         return Error;
  211.     }
  212. }
  213.